package com.ruoyi.pay.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.pay.entity.OrderInfo;
import com.ruoyi.pay.entity.PaymentInfo;
import com.ruoyi.pay.enums.AliPayTradeStatus;
import com.ruoyi.pay.enums.OrderStatus;
import com.ruoyi.pay.enums.PayType;
import com.ruoyi.pay.mapper.OrderInfoMapper;
import com.ruoyi.pay.mapper.PaymentInfoMapper;
import com.ruoyi.pay.service.PaymentInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static net.sf.jsqlparser.parser.feature.Feature.insert;

@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Resource
    private PaymentInfoMapper paymentInfoMapper;

    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Override
    public void insertPaymentInfo(PaymentInfo paymentInfo) {
        int insert = paymentInfoMapper.insert(paymentInfo);

        if(insert < 1){
            throw new RuntimeException("插入支付信息失败");
        }
    }


    @Override
    public void insertPaymentInfoByOrderInfo(HashMap<String, String> params, String orderNo) {
        // 获取params中的信息
        String tradeNo = params.get("trade_no");    // 支付系统交易编号
        String totalAmount = params.get("total_amount");    // 交易金额


        // 根据orderNo查询订单信息
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderInfo::getOrderNo, orderNo);
        OrderInfo orderInfo = orderInfoMapper.selectOne(wrapper);

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderInfo.getOrderNo());
        paymentInfo.setTransactionId(totalAmount);     // 支付系统交易编号
        paymentInfo.setPaymentType(PayType.ALIPAY.getType());
        paymentInfo.setTradeType("电脑网站支付");
        paymentInfo.setTradeState(OrderStatus.PAYD.getStatus());
        paymentInfo.setPayerTotal(orderInfo.getTotalFee());     // 价格
        paymentInfo.setContent(JSON.toJSONString(params));   // 通知参数

        this.insertPaymentInfo(paymentInfo);
    }

    @Override
    public void insertPaymentInfoByNotify(Map<String, String> params) {

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(params.get("out_trade_no"));
        paymentInfo.setTransactionId(params.get("trade_no"));   // 支付系统交易编号
        paymentInfo.setPaymentType(PayType.ALIPAY.getType());
        paymentInfo.setTradeType("电脑网站支付");

        // 支付状态是否成功
        // 因为前面已经做了支付成功的校验 所以此处必定成功!
        // 如果成功 给予本地字典值
        paymentInfo.setTradeState(OrderStatus.PAYD.getStatus());

        String totalAmount = params.get("total_amount");
        BigDecimal decimal = new BigDecimal(totalAmount);
        paymentInfo.setPayerTotal(decimal);

        String content = JSON.toJSONString(params);
        paymentInfo.setContent(content);
        this.insertPaymentInfo(paymentInfo);
    }
}
